using UnityEngine;
namespace UnityEngine 
{

[AddComponentMenu("")]
class UserAuthorizationDialog : MonoBehaviour
{
#if !UNITY_FLASH && !UNITY_WEBGL && !UNITY_WINRT
	private Rect windowRect;
	private const int width = 385;
	private const int height = 155;
	
	private Texture warningIcon;
#endif
		
	private void Start ()
	{
#if !UNITY_FLASH && !UNITY_WEBGL && !UNITY_WINRT
		warningIcon = Resources.GetBuiltinResource(typeof (Texture2D), "WarningSign.psd") as Texture2D;
		if (Screen.width < width || Screen.height < height)
		{
			Debug.LogError ("Screen is to small to display authorization dialog. Authorization denied.");
			Application.ReplyToUserAuthorizationRequest (false);
		}
		
		windowRect = new Rect (Screen.width / 2 - width/2, Screen.height / 2 - height/2, width, height);
#endif
	}
		
	private void OnGUI () {
#if !UNITY_FLASH && !UNITY_WEBGL && !UNITY_WINRT
		GUISkin oldSkin = GUI.skin;
		
		// Create a custom GUISkin from scratch so users cannot change 
		// the look of the security dialog by changing the default GUI skins.
		GUISkin skin = ScriptableObject.CreateInstance ("GUISkin") as GUISkin;
		
		skin.box.normal.background = (Texture2D)Resources.GetBuiltinResource(typeof(Texture2D), "GameSkin/box.png");
		skin.box.normal.textColor = new Color (0.9f, 0.9f, 0.9f, 1.0f);
		skin.box.padding.left = 6;
		skin.box.padding.right = 6;
		skin.box.padding.top = 4;
		skin.box.padding.bottom = 4;
		skin.box.border.left = 6;
		skin.box.border.right = 6;
		skin.box.border.top = 6;
		skin.box.border.bottom = 6;
		skin.box.margin.left = 4;
		skin.box.margin.right = 4;
		skin.box.margin.top = 4;
		skin.box.margin.bottom = 4;
		
		skin.button.normal.background = (Texture2D)Resources.GetBuiltinResource(typeof(Texture2D), "GameSkin/button.png");
		skin.button.normal.textColor = new Color (0.9f, 0.9f, 0.9f, 1.0f);
		skin.button.hover.background = (Texture2D)Resources.GetBuiltinResource(typeof(Texture2D), "GameSkin/button hover.png");
		skin.button.hover.textColor = Color.white;
		skin.button.active.background = (Texture2D)Resources.GetBuiltinResource(typeof(Texture2D), "GameSkin/button active.png");
		skin.button.active.textColor = new Color (0.9f, 0.9f, 0.9f, 1.0f);
		skin.button.border.left = 6;
		skin.button.border.right = 6;
		skin.button.border.top = 6;
		skin.button.border.bottom = 6;
		skin.button.padding.left = 8;
		skin.button.padding.right = 8;
		skin.button.padding.top = 4;
		skin.button.padding.bottom = 4;
		skin.button.margin.left = 4;
		skin.button.margin.right = 4;
		skin.button.margin.top = 4;
		skin.button.margin.bottom = 4;
		
		skin.label.normal.textColor = new Color (0.9f, 0.9f, 0.9f, 1.0f);
		skin.label.padding.left = 6;
		skin.label.padding.right = 6;
		skin.label.padding.top = 4;
		skin.label.padding.bottom = 4;
		skin.label.margin.left = 4;
		skin.label.margin.right = 4;
		skin.label.margin.top = 4;
		skin.label.margin.bottom = 4;
		skin.label.alignment = TextAnchor.UpperLeft;

		skin.window.normal.background = (Texture2D)Resources.GetBuiltinResource(typeof(Texture2D), "GameSkin/window.png");
		skin.window.normal.textColor = Color.white;
		skin.window.border.left = 8;
		skin.window.border.right = 8;
		skin.window.border.top = 18;
		skin.window.border.bottom = 8;
		skin.window.padding.left = 8;
		skin.window.padding.right = 8;
		skin.window.padding.top = 20;
		skin.window.padding.bottom = 5;
		skin.window.alignment = TextAnchor.UpperCenter;
		skin.window.contentOffset = new Vector2 (0, -18);

		GUI.skin = skin;
	    windowRect = GUI.Window (0, windowRect, DoUserAuthorizationDialog, "Unity Web Player Authorization Request");
		GUI.skin = oldSkin;
#endif
	}
	
	// Make the contents of the window
	private void DoUserAuthorizationDialog (int windowID) {
#if !UNITY_FLASH && !UNITY_WEBGL && !UNITY_WINRT
		UserAuthorization auth = Application.GetUserAuthorizationRequestMode();

		GUILayout.FlexibleSpace ();
		
		GUI.backgroundColor = new Color (0.9f, 0.9f, 0.9f, 0.7f);
		GUILayout.BeginHorizontal ("box");
		GUILayout.FlexibleSpace ();
		
		GUILayout.BeginVertical();
		GUILayout.FlexibleSpace ();
		
		GUILayout.Label(warningIcon);
		
		GUILayout.FlexibleSpace ();
		GUILayout.EndVertical();
			
		GUILayout.FlexibleSpace ();
		
		GUILayout.BeginVertical();
		GUILayout.FlexibleSpace ();
		
		if (auth == (UserAuthorization.WebCam | UserAuthorization.Microphone))
			GUILayout.Label ("The content on this site would like to use your\ncomputer's web camera and microphone.\nThese images and sounds may be recorded.");
		else
		if (auth == UserAuthorization.WebCam)
			GUILayout.Label ("The content on this site would like to use\nyour computer's web camera. The images\nfrom your web camera may be recorded.");
		else
		if (auth == UserAuthorization.Microphone)
			GUILayout.Label ("The content on this site would like to use\nyour computer's microphone. The sounds\nfrom your microphone may be recorded.");
		else
			return;	
		
		GUILayout.FlexibleSpace ();
		GUILayout.EndVertical();
		
		GUILayout.FlexibleSpace ();
		GUILayout.EndHorizontal ();	
		
		GUILayout.FlexibleSpace ();
		
		GUI.backgroundColor = Color.white;
		GUILayout.BeginHorizontal ();
		
		if (GUILayout.Button ("Deny"))
			Application.ReplyToUserAuthorizationRequest (false);
			
		GUILayout.FlexibleSpace ();
		
		if (GUILayout.Button ("Always Allow for this Site"))
			Application.ReplyToUserAuthorizationRequest (true, true);
		
		GUILayout.Space (5);
		
		if (GUILayout.Button ("Allow"))
			Application.ReplyToUserAuthorizationRequest (true);
		
		GUILayout.EndHorizontal ();
		GUILayout.FlexibleSpace ();
#endif
	}
}
}
